/* 
 * File: timespanstablemodel.cpp
 * Project: QTimeTracker
 * Author: Steve Sindelar, http://stevesindelar.cz
 * Licence: NewBSD licence.
 */

#include "timespanstablemodel.h"
#include "lengtheditor.h"
#include <QtAlgorithms>

using namespace Ui;

//----------------------------------------------------------------------------------------------------------//
//------------------------- FromColumn --------------------------------//

TimeSpansTableModel::FromColumn::FromColumn(TimeSpansTableModel *model)
{
    this->model = model;
}

QString TimeSpansTableModel::FromColumn::getHeader() const
{
    return tr("From");
}

QVariant TimeSpansTableModel::FromColumn::getDataForEditRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].getFrom();
}

QVariant TimeSpansTableModel::FromColumn::getDataForDisplayRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].getFrom();
}

bool TimeSpansTableModel::FromColumn::setData(const QModelIndex &index, const QVariant &value) const
{
    TimeSpan* s = &this->model->getSpans()[index.row()];
    this->model->getSpans()[index.row()].setFrom(value.toDateTime());
    return true;
}

Qt::ItemFlags TimeSpansTableModel::FromColumn::getFlags(const QModelIndex & index) const
{
    return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

//----------------------------------------------------------------------------------------------------------//
//------------------------- ToColumn --------------------------------//

TimeSpansTableModel::ToColumn::ToColumn(TimeSpansTableModel *model)
{
    this->model = model;
}

QString TimeSpansTableModel::ToColumn::getHeader() const
{
    return tr("To");
}

QVariant TimeSpansTableModel::ToColumn::getDataForEditRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].getTo();
}

QVariant TimeSpansTableModel::ToColumn::getDataForDisplayRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].getTo();
}

bool TimeSpansTableModel::ToColumn::setData(const QModelIndex &index, const QVariant &value) const
{
    this->model->getSpans()[index.row()].setTo(value.toDateTime());
    return true;
}

Qt::ItemFlags TimeSpansTableModel::ToColumn::getFlags(const QModelIndex & index) const
{
    if (model->getSpans()[index.row()].isRunning())
    {
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    }
    else 
    {
        return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    }
}

//----------------------------------------------------------------------------------------------------------//
//------------------------- LengthColumn --------------------------------//

TimeSpansTableModel::LengthColumn::LengthColumn(TimeSpansTableModel *model)
{
    this->model = model;
}

QString TimeSpansTableModel::LengthColumn::getHeader() const
{
    return tr("Length");
}

QVariant TimeSpansTableModel::LengthColumn::getDataForEditRole(const QModelIndex &index) const
{
    return (int)this->model->getSpans()[index.row()].getLength().getSecs();
}

QVariant TimeSpansTableModel::LengthColumn::getDataForDisplayRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].getLength().toString();
}

bool TimeSpansTableModel::LengthColumn::setData(const QModelIndex &index, const QVariant &value) const
{
    TimeSpanData d = TimeSpanData::fromSeconds(value.toInt());
    this->model->getSpans()[index.row()].setLength(d.hours, d.minutes, d.seconds);
    return true;
}

Qt::ItemFlags TimeSpansTableModel::LengthColumn::getFlags(const QModelIndex & index) const
{
    return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

//----------------------------------------------------------------------------------------------------------//
//------------------------- NoteColumn --------------------------------//

TimeSpansTableModel::NoteColumn::NoteColumn(TimeSpansTableModel *model)
{
    this->model = model;
}

QString TimeSpansTableModel::NoteColumn::getHeader() const
{
    return tr("Note");
}

QVariant TimeSpansTableModel::NoteColumn::getDataForEditRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].note;
}

QVariant TimeSpansTableModel::NoteColumn::getDataForDisplayRole(const QModelIndex &index) const
{
    return this->model->getSpans()[index.row()].note;
}

bool TimeSpansTableModel::NoteColumn::setData(const QModelIndex &index, const QVariant &value) const
{
    this->model->getSpans()[index.row()].note = value.toString();
    return true;
}

Qt::ItemFlags TimeSpansTableModel::NoteColumn::getFlags(const QModelIndex & index) const
{
    return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
